<?php

class Bbling_AjaxController extends Zend_Controller_Action
{
  
    public function init()
    {
        /* Initialize action controller here */
        
    }
	
	public function addFinancialPlanDataAction() {	
		
		$startmonth= $_POST['startfinancialyear'];
		$startbalance= $_POST['start_balance'];
		$id = $_POST['finance_id'];		
		$model = new Bbling_Model_Entity_Financialplan;
		
		
		if($id == 0)
		{
			$id = $model->business_check_record($_POST['user_id']);
			if(!$id)
			{
				$id=$model->save_financial($startmonth,$startbalance);
				$marketing_id = $model->save_marketing();
				$company_id=$model->save_company();
				$product_id=$model->save_product();
				$model->create_business_record($_POST['user_id'],$id,$marketing_id,$company_id,$product_id);
			}
									
		}
		else
		{
			unset($_POST['finance_id']);
			$data['financial_start_month']=$_POST['startfinancialyear'];
		    $data['starting_balance']=$_POST['start_balance'];
			$model->updateByFinancialId($data,$id);	
		}
		
		echo $id;
		exit();
	}
	
	
	public function addUpdateFinancialPlanAction()
	{
		$model = new Bbling_Model_Entity_Financialplan;
		$model->changetable('product_financial');
		$financial_id = $_POST['finance_id'];
		$model->deleteByFinancialId($financial_id);
		$products_raw = explode("\r\n",$_POST['products']);
		
		foreach($products_raw as $product_raw)
		{						
			$product = array();
			$product_parts = explode("|",$product_raw);
			if(count($product_parts) < 16)
			continue;
			$product['financial_id'] = $financial_id;
			$product['product_name'] = trim($product_parts[0]);
			$product['product_charge'] = trim($product_parts[1]);
			$product['product_cost'] = trim($product_parts[2]);
			$product['product_or_service'] = trim($product_parts[3]);
			$product['january'] = trim($product_parts[4]);
			$product['february'] = trim($product_parts[5]);
			$product['march'] = trim($product_parts[6]);
			$product['april'] = trim($product_parts[7]);
			$product['may'] = trim($product_parts[8]);
			$product['june'] = trim($product_parts[9]);
			$product['july'] = trim($product_parts[10]);
			$product['august'] = trim($product_parts[11]);
			$product['september'] = trim($product_parts[12]);
			$product['octuber'] = trim($product_parts[13]);
			$product['november'] = trim($product_parts[14]);
			$product['december'] = trim($product_parts[15]);
			$model->insertNewRecord($product);
		}
		
		$model->changetable('off_cost');
		$model->deleteByFinancialId($financial_id);
		$oocs_raw = explode("\r\n",$_POST['oocs']);
		foreach($oocs_raw as $ooc_raw)
		{
		  $ooc = array();
		  $ooc_parts = explode("|",$ooc_raw);
		  if(count($ooc_parts) < 3)
		  continue;
		  
		  $ooc['financial_id'] = $financial_id;
		  $ooc['what_for'] =  trim($ooc_parts[0]);
		  $ooc['how_much'] =  trim($ooc_parts[1]);
		  $ooc['month'] =  trim($ooc_parts[2]);
		  $model->insertNewRecord($ooc);
		}
		
		$model->changetable('financial_monthly_expenses');
		$model->deleteByFinancialId($financial_id);
		$data = $_POST['chart'];
		$data['financial_id'] = $financial_id;
	    $model->insertNewRecord($data);
		exit();
	}
	
}
?>